package com.yangye.pidoublename.controller;

import com.yangye.picommon.exception.PiException;
import com.yangye.picommon.model.PiResult;
import com.yangye.pidoublename.service.DoubleNameService;
import com.yangye.pidoublename.service.DoubleNameServiceFactory;
import com.yangye.pidoublename.service.ProvinceEnum;
import com.yangye.pidoublename.vo.DoubleNameVo;
import com.yangye.pidoublename.vo.ProvinceVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangye
 * @date 2019/9/12
 */
@RestController
@RequestMapping("/open/doublename")
public class DoubleNameController {

	@Autowired
	private DoubleNameServiceFactory factory;

	@GetMapping("/query")
	public PiResult<DoubleNameVo> findDoubleName(@RequestParam("name") String name,
												 @RequestParam("province") String province) {
		if (StringUtils.isEmpty(name) || StringUtils.isEmpty(province)) {
			throw new PiException("请求参数不能为空");
		}
		DoubleNameService doubleNameService = factory.getDoubleNameService(province);
		return PiResult.succeed(doubleNameService.findByName(name));
	}

	@GetMapping("/provinces")
	public PiResult findProvinces() {
		List<ProvinceVo> provinceVos = new ArrayList<>();
		for (ProvinceEnum value : ProvinceEnum.values()) {
			ProvinceVo provinceVo = new ProvinceVo();
			provinceVo.setName(value.getName());
			provinceVo.setCode(value.getCode());
			provinceVos.add(provinceVo);
		}
		return PiResult.succeed(provinceVos);
	}
}
